import Taro, {Component, Config} from '@tarojs/taro'
import Index from './pages/index'

import './assets/font/iconfont.css'
import './app.scss'
import {setGlobalData} from './utils/global'

import {Provider} from '@tarojs/redux'

import lovelornUser from './api/lovelorn-user'

import configStore from './store'

const store = configStore

class App extends Component {
  // constructor(props) {
  //   super(props)
  //   this.state = {
  //     loginResult: {},
  //     code: '',
  //     height:''
  //   }
  // }
  globalData = {
    loginType: false,
    userInfo: {},
    loginVO: {},
  }

  loginWx = () => {
    return new Promise((resolve, reject) => {
      Taro.login({
        success: res => {
          resolve(this.loginThird(res.code))
        },
        fail: error => {
          console.log(error, 'error')
          Taro.showToast({
            title: '微信登录失败',
          })
        },
      })
    })
  }
  getWindowHeight = () => {
    Taro.getSystemInfo({
      success: function(res) {
        console.log(res, 'res.windowHeight')
        setGlobalData('windowHeight', res.windowHeight)
        setGlobalData('statusBarHeight', res.statusBarHeight)
        let isIos = res.system.toUpperCase().indexOf('IOS') > -1 ? true : false
        // 标题栏高度：安卓：48px，iOS：44px
        setGlobalData('isIos', isIos)
        setGlobalData('navigatetionHeight', isIos ? 44 : 48)
      },
      fail: function() {
        setGlobalData('windowHeight', 600)
      },
    })
  }
  loginThird = code => {
    lovelornUser.action
      .login({
        code,
      })
      .then(result => {
        if (result && result.checkFlag) {
          console.log('用户登录成功')
          setGlobalData('loginVO', result.loginVO)
          Taro.setStorage({
            key: 'isLogin',
            data: true,
          })
          Taro.setStorage({
            key: 'loginVO',
            data: result.loginVO,
          })
        } else {
          console.log('用户登录失败')
          Taro.setStorage({
            key: 'isLogin',
            data: false,
          })
        }
      })
  }

  componentDidMount() {
    // this.loginWx()

    // let that = this;
    // Taro.getStorage({
    //   key:'isLogin',
    //   success:function(res){
    //       if(res.data){
    //         that.loginWx()
    //       }
    //   },
    // })
    this.getWindowHeight()
  }

  componentDidShow() {}

  componentDidHide() {}

  componentDidCatchError() {}

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    pages: [
      'pages/userLogin/index',
      'pages/Home/index',
      'pages/MyInfo/index',
      'pages/MyLabel/index',
      'pages/Mine/index',
      'pages/userRegister/index',
      'pages/MenberHomePage/index',
      'pages/VideoDetail/index',
      'pages/Desquamation/index',
    ],

    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: '征婚',
      navigationBarTextStyle: 'black',
    },
    tabBar: {
      borderStyle: 'black',
      selectedColor: '#FF774D',
      backgroundColor: '#ffffff',
      color: '#999',
      list: [
        {
          pagePath: 'pages/Home/index',
          selectedIconPath: './assets/seekingActive.png',
          iconPath: './assets/seeking.png',
          text: '征婚',
        },
        {
          pagePath: 'pages/Mine/index',
          selectedIconPath: './assets/myActive.png',
          iconPath: './assets/my.png',
          text: '我的',
        },
      ],
    },

    permission: {
      'scope.userLocation': {
        desc: '小程序需要获取你的当前位置信息', // 高速公路行驶持续后台定位
      },
    },
  }

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render() {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
